package iSOCK::Controller::Specification::Diff;

use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller' }

use iSOCK::Controller::Root;

=head1 NAME

iSOCK::Controller::Diff - Diff Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub index :Path {
  my ( $self, $c ) = @_;
  
  my $parameters = $c->request->parameters;
  my $error      = sub { $c->stash ( body => "<p class=\"error\">ERROR! $_[0]</p>" ) };
  
  if ( my $session = $c->session ) {
    my ( $product, $disable );
    
    my $specifications = $c->model ( 'App' )->specifications_loh ( $c );
    
    my $has_api_files = $session->{svn_product} && $session->{api_h_file} && $session->{api_svd_file};
    
    if ( $has_api_files ) {
      $product        = $session->{svn_product};       
      $disable        = { product => 1, submit => 0 }; 
      
      $specifications = [ grep { $_->{id} eq $product } @$specifications ];
    }
    else {
      $product        = $parameters->{specification};
      $disable        = { product => 0, submit => 1 };
    }
    
    $c->stash ( specifications => $specifications, disable => $disable );

    if ( $c->request->method eq 'POST' && $parameters->{form_diff_submit} ) {
      $c->model ( 'Product' )->diff ({ c => $c, view => $parameters->{view}, svn_product => $product, old_tag => $parameters->{old_tag}, new_tag => $parameters->{new_tag}, has_api_files => $has_api_files });
    }
    elsif ( $has_api_files ) {
      $c->model ( 'Product' )->diff ({ c => $c, view => $parameters->{view}, svn_product => $product, old_tag =>    $session->{svn_tag}, new_tag => 'current',              has_api_files => $has_api_files });
    }
  }
  
  $c->forward ( qw( iSOCK::Controller::Root render_view ) );
}

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
